package com.newzqxq.koalamoney.widget.toast;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.annotation.DrawableRes;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.newzqxq.koalamoney.R;


/**
 * 自定义 Toast ——单例
 * <p>
 * 方法：
 * <p>
 * {@link #setLogo(int) 设置图标 }
 * {@link #setMessage(CharSequence) 设置提示文字 }
 * {@link #show() 显示 }
 * {@link #cancel() 取消 }
 * <p>
 * Created by 叶开 on 2017/6/7.
 */

final class HintToast extends Toast {

    /**
     * Construct an empty Toast object.  You must call {@link #setView} before you
     * can call {@link #show}.
     *
     * @param context The context to use.  Usually your {@link Application}
     *                or {@link Activity} object.
     */
    public HintToast(Context context) {
        super(context);

        //屏幕宽度
        int WIDTH = metrics(context).widthPixels;
        //屏幕高度
        int HEIGHT = metrics(context).heightPixels;

        View view = LayoutInflater.from(context).inflate(R.layout.toast_hinter, null);

        LinearLayout root = (LinearLayout) view.findViewById(R.id.toast_hinter_lin_root);
        root.setLayoutParams(new FrameLayout.LayoutParams(
                WIDTH / 2,
                HEIGHT / 4
        ));

        imgLogo = (ImageView) view.findViewById(R.id.toast_hinter_img_logo);
        tvMessage = (TextView) view.findViewById(R.id.toast_hinter_tv_message);

        setDuration(Toast.LENGTH_SHORT);
        setGravity(Gravity.CENTER, 0, 0);

        setView(view);
    }

    private ImageView imgLogo;//图标控件
    private TextView tvMessage;//文字控件

    /**
     * @param drawable 设置 logo
     */
    HintToast setLogo(@DrawableRes int drawable) {
        imgLogo.setImageResource(drawable);
        return this;
    }

    /**
     * @param message 设置文字
     */
    HintToast setMessage(CharSequence message) {
        if (message != null) {
            tvMessage.setText(message);
        }
        return this;
    }

    //TODO 特殊设置加载提示
    HintToast setLoading(@DrawableRes int drawable,
                         CharSequence text) {

        return this;
    }

    @Override
    public void cancel() {
        imgLogo.clearAnimation();
        tvMessage.setText("");
        super.cancel();
    }

    private DisplayMetrics metrics(Context context) {
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics metrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(metrics);
        return metrics;
    }

}
